<template>
  <div class="upload">
    <el-upload
      ref="uploadRef"
      v-model:file-list="defaultFileList"
      :accept="accept"
      :auto-upload="false"
      :disabled="!editable"
      :limit="limit"
      :multiple="false"
      :on-change="handleChange"
      :on-exceed="handleExceed"
      :on-remove="handleRemove"
    >
      <el-button v-show="editable" :icon="Upload" size="small" type="primary"> 选择文件 </el-button>
    </el-upload>
  </div>
</template>

<script>
import { Download, Upload } from "@element-plus/icons-vue";
import VabDialog from "@/plugins/VabDialog/index.vue";

export default defineComponent({
  name: "UploadEmbed",
  components: {},
  props: {
    accept: {
      type: String,
      default: ""
    },
    limit: {
      type: Number,
      default: 1
    },
    defaultfilelist: {
      type: Array,
      default: () => []
    },
    editable: {
      type: Boolean,
      default: true
    },
    size: {
      type: Number,
      default: 1024,
      required: true
    }
  },
  setup(props) {
    const { proxy } = getCurrentInstance();
    const state = reactive({
      loading: false,
      uploadRef: null,
      title: "上传",
      fileList: [],
      defaultFileList: [],
      delFileList: [],
      editable: true,
      limit: "1",
      accept: ""
    });

    state.accept = ref(props.accept);
    state.limit = ref(props.limit);
    state.defaultFileList = ref(props.defaultfilelist);
    state.editable = ref(props.editable);
    state.size = ref(props.size);

    const handleChange = file => {
      if (file.size > state.size) {
        proxy.$baseMessage(`文件大于${state.size}B`, "warning");
        //state['uploadRef'].uploadFiles.splice(0, 1)
        state["uploadRef"].handleRemove(file);
        return false;
      } else {
        state.fileList.push(file);
        return true;
      }
    };

    const handleRemove = file => {
      if (file.raw == null) {
        state.delFileList.push(file);
      }
      const newFile = [];
      state.fileList.forEach(f => {
        if (f.uid != file.uid) {
          if (f.raw != null) {
            newFile.push(f);
          }
        }
      });
      state.fileList = newFile;
    };

    const handleExceed = files => {
      if (state.limit == 1) {
        //替换当前的文件
        state.fileList = [];
        state.fileList.push(files[0]);
      } else {
        proxy.$baseMessage(
          `当前限制选择 ${state.limit} 个文件，本次选择了
                ${files.length}
                个文件`,
          "error",
          "vab-hey-message-error"
        );
      }
    };

    const getFileList = () => {
      //return state.fileList
      return { fileList: state.fileList, delList: state.delFileList };
    };

    watch(
      () => props.defaultfilelist,
      newVal => {
        state.defaultFileList = ref(newVal);
        state.fileList = [];
      }
    );

    return {
      ...toRefs(state),
      getFileList,
      handleChange,
      handleExceed,
      handleRemove,
      Download,
      Upload,
      VabDialog
    };
  }
});
</script>

<style lang="scss" scoped>
.upload {
  .upload-content {
    .el-upload__tip {
      display: block;
      height: 30px;
      line-height: 30px;
    }
    :deep() {
      .el-upload--picture-card {
        width: 128px;
        height: 128px;
        margin: 3px 8px 8px;
        border: 2px dashed #c0ccda;
      }
      .el-upload-list--picture {
        margin-bottom: 20px;
      }
      .el-upload-list--picture-card {
        .el-upload-list__item {
          width: 128px;
          height: 128px;
          margin: 3px 8px 8px;
        }
      }
    }
  }
}
.text-left {
  padding-left: 30px;
  text-align: left;
}
h3 {
  padding-bottom: 20px;
}
.cust_btn30 {
  padding-top: 30px;
  padding-bottom: 60px;
}
.text_fise {
  font-size: 16px;
}
.text_fise > div {
  padding-bottom: 20px;
}
</style>
